<template>
  <div class="hello">
        <home-header></home-header>
        <home-swiper :swiperList="swiperList"></home-swiper>
        <home-icons :iconList="iconList"></home-icons>
        <recommend :infoList='infoList'></recommend>
        <weekend :weekendList='weekendList'></weekend>
  </div>
</template>

<script>
import axios from 'axios'
import { mapState } from 'vuex'
import HomeHeader from './components/header.vue'
import homeSwiper from './components/homeSwiper.vue'
import homeIcons from './components/Icons.vue'
import recommend from './components/recommend.vue'
import weekend from './components/weekend.vue'
export default {
  name: 'Home',
  components: {
    HomeHeader,
    homeSwiper,
    homeIcons,
    recommend,
    weekend
  },
  data () {
    return {
      swiperList: [],
      iconList: [],
      infoList: [],
      weekendList: [],
      lastCity: ''
    }
  },
  computed: {
    ...mapState(['city'])
  },
  methods: {
    getHomeInfo () {
      axios.get('/api/index.json?city=' + this.city)
        .then(this.getHomeInfoSucc)
      this.lastCity = this.city
    },
    getHomeInfoSucc (res) {
      res = res.data
      if (res.ret && res.data) {
        const data = res.data
        this.swiperList = data.swiperList
        this.iconList = data.iconList
        this.infoList = data.recommendList
        this.weekendList = data.weekendList
      }
    }
  },
  mounted () {
    this.getHomeInfo()
  },
  activated () {
    if (this.lastCity !== this.city) {
      this.getHomeInfo()
    }
  }

}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
